﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using Com.Crc.ResManage.BLL.EquipManage;
using Com.Crc.PoJo.Model.Resource;
using Com.App_Code;
using Infragistics.WebUI.UltraWebGrid;

namespace Com.Crc.ResManage.Web.EquipManage.PON.ONU
{
    public partial class PONUserBoardONUManageSub : BasePage
    {
        ONUManageBLL bllONU;
        OLTManageBLL bllOLT;
        PONManageUserBoardBLL bllPONUserBoard;

        public string ONUID
        {
            get
            {
                if (ViewState["ONUID"] != null)
                {
                    return ViewState["ONUID"].ToString();
                }

                return "";
            }
            set
            {
                ViewState["ONUID"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitControl();
            }
        }

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            bllONU = new ONUManageBLL();
            bllOLT = new OLTManageBLL();

            if (Request.QueryString["onuid"] != null)
            {
                ONUID = Request.QueryString["onuid"].ToString();
            }

            //初始化各个下拉框
            InitControlHelp.LoadSubDistrictInfo(this.ddlSubdistrict);
            InitControlHelp.LoadBranchInfo(this.ddlOffice);
            InitControlHelp.LoadCommonDicInfo(this.ddlBranch, Dictionary.COMMON_DIC_TYPE.RESOURCE_WLAN_BRANCH_TYPE);
            InitControlHelp.LoadCommonDicInfo(this.ddlONUType, Dictionary.COMMON_DIC_TYPE.RESOURCE_ONU_EQUIPMENT_TYPE);
            DataTable dtOLT = bllOLT.QueryOLTData("", "", "");
            ddlUpOLT.DataSource = dtOLT;
            ddlUpOLT.DataTextField = "s_name";
            ddlUpOLT.DataValueField = "n_id";
            ddlUpOLT.DataBind();
            
            //初始化PON板端口
            this.ddlPonPort.Items.AddRange(new ListItem[]
            {
                new ListItem("端口0","0"),
                new ListItem("端口1","1"),
                new ListItem("端口2","2"),
                new ListItem("端口3","3"),
                new ListItem("端口4","4"),
                new ListItem("端口5","5"),
                new ListItem("端口6","6"),
                new ListItem("端口7","7")
            });

            if (Request.QueryString["optype"] != null && Request.QueryString["optype"].ToString() == "0")
            {
                LoadEquipData();
            }
        }

        /// <summary>
        /// 修改设备信息时初始化控件数据
        /// </summary>
        private void LoadEquipData()
        {
            bllONU = new ONUManageBLL();
            DataTable dt = bllONU.QueryONUData("", "", "",ONUID);
            InitControlHelp.SetSelectedValue(this.ddlONUType, dt.Rows[0]["N_UNITTYPE_ID"].ToString());
            InitControlHelp.SetSelectedValue(this.ddlSubdistrict, dt.Rows[0]["N_SUBDISTRICT_ID"].ToString());
            InitControlHelp.SetSelectedValue(this.ddlUpOLT, dt.Rows[0]["N_OLT_ID"].ToString());
            this.txtSetupAddress.Text = dt.Rows[0]["S_ADDRESS"].ToString();
            this.txtIPAddress.Text = dt.Rows[0]["S_MANGER"].ToString();
            this.txtRemark.Text = dt.Rows[0]["S_BAK"].ToString();
            this.txtMac.Text = dt.Rows[0]["S_MAC"].ToString();
            this.txtVlan.Text = dt.Rows[0]["N_VLAN"].ToString();
            this.txtBeginVlan.Text = dt.Rows[0]["N_S_IDX"].ToString();
            this.txtEndVlan.Text = dt.Rows[0]["N_E_IDX"].ToString();
            InitControlHelp.SetSelectedValue(this.ddlPonPort, dt.Rows[0]["N_PON_PORT"].ToString());
            LoadUserBoardInfo(dt.Rows[0]["N_OLT_ID"].ToString());
            InitControlHelp.SetSelectedValue(this.ddlPonBoard, dt.Rows[0]["N_USERBOARD_ID"].ToString());
        }
        
        /// <summary>
        /// 新增或修改保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {

            bllONU = new ONUManageBLL();
            ONUEntity onuData = new ONUEntity();
            onuData.N_DEP_BRANCH_ID = long.Parse(ddlBranch.Items[this.ddlBranch.SelectedIndex].Value);
            onuData.N_SUBSTATION_ID = long.Parse(ddlOffice.Items[this.ddlOffice.SelectedIndex].Value);
            onuData.N_UNITTYPE_ID = long.Parse(ddlONUType.Items[this.ddlONUType.SelectedIndex].Value);
            onuData.N_SUBDISTRICT_ID = long.Parse(ddlSubdistrict.Items[this.ddlSubdistrict.SelectedIndex].Value);
            onuData.S_ADDRESS = txtSetupAddress.Text.Trim();
            onuData.S_MANGER = txtIPAddress.Text.Trim();
            onuData.N_OLT_ID = long.Parse(ddlUpOLT.Items[this.ddlUpOLT.SelectedIndex].Value);
            onuData.N_USERBOARD_ID = long.Parse(ddlPonBoard.Items[this.ddlPonBoard.SelectedIndex].Value);
            onuData.N_PON_PORT = long.Parse(ddlPonPort.Items[this.ddlPonPort.SelectedIndex].Value);
            onuData.N_S_IDX = long.Parse(txtBeginVlan.Text.Trim());
            onuData.N_E_IDX = long.Parse(txtEndVlan.Text.Trim());
            onuData.S_BAK = txtRemark.Text.Trim();
            onuData.S_STATE = "1";
            onuData.D_DATE = DateTime.Now;
            onuData.S_MAC = this.txtMac.Text;
            onuData.N_VLAN = long.Parse(txtVlan.Text.Trim());

            if (Request.QueryString["optype"] != null && Request.QueryString["optype"].ToString() == "0")
            {
                //修改
                onuData.N_ID = long.Parse(this.ONUID);
                bllONU.UpdateONUData(onuData);
                base.ShowAlertForUpdatePanel("设备修改成功！");
            }
            else
            {
                //新增
                bllONU.AddONUData(onuData);

                base.ShowAlertForUpdatePanel("设备新增成功！");
                this.btnAdd.Enabled = false;
            }
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            this.txtBeginVlan.Text = "";
            this.txtEndVlan.Text = "";
            this.txtIPAddress.Text = "";
            this.txtMac.Text = "";
            this.txtOfficeNo.Text = "";
            this.txtRemark.Text = "";
            this.txtSetupAddress.Text = "";
            this.txtVlan.Text = "";
        }

        protected void ddlUpOLT_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadUserBoardInfo(ddlUpOLT.SelectedValue);
        }

        private void LoadUserBoardInfo(string strOLTID)
        {
            bllPONUserBoard = new PONManageUserBoardBLL();
            //加载PON板信息
            DataTable dtPonBoard = bllPONUserBoard.QueryOLTUserBoardData("", "", strOLTID, "");
            DataTable dtPonSlot = new DataTable();
            dtPonSlot.Columns.AddRange(new DataColumn[] { 
                new DataColumn("S_NAME",typeof(string)),
                new DataColumn("N_ID",typeof(int))
            });
            dtPonSlot.Rows.Clear();
            foreach (DataRow row in dtPonBoard.Rows)
            {
                DataRow rowNew = dtPonSlot.NewRow();
                rowNew["S_NAME"] = "第" + row["N_SLOT_NO"].ToString() + "槽位PON板";
                rowNew["N_ID"] = row["N_ID"].ToString();
                dtPonSlot.Rows.Add(rowNew);
            }
            ddlPonBoard.DataSource = dtPonSlot;
            ddlPonBoard.DataTextField = "S_NAME";
            ddlPonBoard.DataValueField = "N_ID";
            ddlPonBoard.DataBind();
        }
    }
}